package com.qingzhuge.pageview.service;

import com.qingzhuge.pageview.dao.ISysPageViewDao;
import com.qingzhuge.pageview.entity.SysPageView;
import com.qingzhuge.service.BasePkLongServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2019/1/17 0017 下午 14:50
 * @modified :
 * @since :
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysPageViewServiceImpl extends BasePkLongServiceImpl<ISysPageViewDao, SysPageView> implements ISysPageViewService {
    @Override
    public int findVpByModuleAndObjId(String module, String objId) {
        int pv = 0;
        if (StringUtils.isNotBlank(module) && StringUtils.isNotBlank(objId)) {
            SysPageView pageView = baseDao.findVpByModuleAndObjId(module, objId);
            if (null != pageView) {
                pv = pageView.getVp();
            }
        }
        return pv;
    }

    @Override
    public SysPageView findByModuleAndObjId(String module, String objId) {
        if (StringUtils.isNotBlank(module) && StringUtils.isNotBlank(objId)) {
            return baseDao.findByModuleAndObjId(module, objId);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVpByModuleAndObjId(String module, String objId) {
        if (StringUtils.isNotBlank(module) && StringUtils.isNotBlank(objId)) {
            SysPageView pageView = findByModuleAndObjId(module, objId);
            if (null == pageView){
                pageView = new SysPageView();
                pageView.setModule(module);
                pageView.setObjId(objId);
                super.save(pageView);
                return 1;
            }else {
                baseDao.updateVpByModuleAndObjId(module, objId);
                return 1;
            }
        }
        return -1;
    }
}
